"""
    在视图函数：
            # 所有数据
            queryset = models.Customer.objects.filter(active=1).select_related('level')

            pager = Pagination(request, queryset)
            return render(request, 'xx.html', {"pager":pager})

    在页面上：
        {% for row in pager.queryset %}
            {{row.id}}
        {% endfor %}

        <ul class="pagination">
            {{ pager.html }}
        </ul>

"""
from copy import deepcopy

from django.utils.safestring import mark_safe


class Pagination(object):
    def __init__(self, request, query_set, per_page_count=10):
        page = request.GET.get('page')
        if not page:
            self.page = 1
        else:
            if not page.isdecimal():
                self.page = 1
            else:
                self.page = int(request.GET.get('page'))

        self.query_set = query_set
        self.query_dict = deepcopy(request.GET)
        self.query_dict._mutable = True
        self.per_page_count = per_page_count
        self.total_count = query_set.count()
        self.start = (self.page - 1) * per_page_count
        self.end = self.page * self.per_page_count
        self.total_page, div = divmod(self.total_count, self.per_page_count)
        if div: self.total_page += 1

    @property
    def html(self):
        page_list = []

        if self.page > 1:
            self.query_dict.setlist('page', [1])
            praramString = self.query_dict.urlencode()
            page_list.append(f"<li><a href='?{praramString}'>首页</a></li>")
            self.query_dict.setlist('page', [self.page - 1])
            praramString = self.query_dict.urlencode()
            page_list.append(f"<li><a href='?{praramString}'>上一页</a></li>")

        if self.page <= 5:
            self.start_page = 1
            self.end_page = 11

        elif self.total_page - self.page < 5:
            self.start_page = self.total_page - 10
            self.end_page = self.total_page

        else:
            self.start_page = self.page - 5
            self.end_page = self.page + 5

        if self.total_page < 11:
            self.start_page = 1
            self.end_page = self.total_page

        for i in range(self.start_page, self.end_page + 1):
            if i == self.page:
                self.query_dict.setlist('page', [i])
                praramString = self.query_dict.urlencode()
                item = f"<li class='active'><a href='?{praramString}'>{i}</a></li>"
            else:
                self.query_dict.setlist('page', [i])
                praramString = self.query_dict.urlencode()
                item = f"<li><a href='?{praramString}'>{i}</a></li>"
            page_list.append(item)

        if self.page < self.total_page:
            self.query_dict.setlist('page', [self.page + 1])
            praramString = self.query_dict.urlencode()
            page_list.append(f"<li><a href='?{praramString}'>下一页</a></li>")
            self.query_dict.setlist('page', [self.total_page])
            praramString = self.query_dict.urlencode()
            page_list.append(f"<li><a href='?{praramString}'>尾页</a></li>")

        page_list.append(f"<li class='disabled'><a>共{self.total_page}页, 当前第{self.page}页</a></li>")

        # mark_safe() 函数的作用是把字符串转换成安全的HTML代码
        page_string = mark_safe(''.join(page_list))

        return page_string

    @property
    def queryset(self):
        return self.query_set[self.start:self.end]
